<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.models.NoisyOrModel &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.models.NoisyOrModel</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>

<span class="kn">from</span> <span class="nn">pgmpy.extern.six.moves</span> <span class="k">import</span> <span class="nb">zip</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern</span> <span class="k">import</span> <span class="n">six</span>


<div class="viewcode-block" id="NoisyOrModel"><a class="viewcode-back" href="../../../models.html#pgmpy.models.NoisyOrModel.NoisyOrModel">[docs]</a><span class="k">class</span> <span class="nc">NoisyOrModel</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for Noisy-Or models.</span>

<span class="sd">    This is an implementation of generalized Noisy-Or models and</span>
<span class="sd">    is not limited to Boolean variables and also any arbitrary</span>
<span class="sd">    function can be used instead of the boolean OR function.</span>

<span class="sd">    Reference: http://xenon.stanford.edu/~srinivas/research/6-UAI93-Srinivas-Generalization-of-Noisy-Or.pdf</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">,</span> <span class="n">inhibitor_probability</span><span class="p">):</span>
        <span class="c1"># TODO: Accept values of each state so that it could be</span>
        <span class="c1"># put into F to compute the final state values of the output</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Init method for NoisyOrModel.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, tuple, dict (array like)</span>
<span class="sd">            array containing names of the variables.</span>

<span class="sd">        cardinality: list, tuple, dict (array like)</span>
<span class="sd">            array containing integers representing the cardinality</span>
<span class="sd">            of the variables.</span>

<span class="sd">        inhibitor_probability: list, tuple, dict (array_like)</span>
<span class="sd">            array containing the inhibitor probabilities of each variable.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import NoisyOrModel</span>
<span class="sd">        &gt;&gt;&gt; model = NoisyOrModel([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], [[0.6, 0.4],</span>
<span class="sd">        ...                                                      [0.2, 0.4, 0.7],</span>
<span class="sd">        ...                                                      [0.1, 0.4]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">inhibitor_probability</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_variables</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">,</span> <span class="n">inhibitor_probability</span><span class="p">)</span>

<div class="viewcode-block" id="NoisyOrModel.add_variables"><a class="viewcode-back" href="../../../models.html#pgmpy.models.NoisyOrModel.NoisyOrModel.add_variables">[docs]</a>    <span class="k">def</span> <span class="nf">add_variables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">,</span> <span class="n">inhibitor_probability</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Adds variables to the NoisyOrModel.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, tuple, dict (array like)</span>
<span class="sd">            array containing names of the variables that are to be added.</span>

<span class="sd">        cardinality: list, tuple, dict (array like)</span>
<span class="sd">            array containing integers representing the cardinality</span>
<span class="sd">            of the variables.</span>

<span class="sd">        inhibitor_probability: list, tuple, dict (array_like)</span>
<span class="sd">            array containing the inhibitor probabilities corresponding to each variable.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import NoisyOrModel</span>
<span class="sd">        &gt;&gt;&gt; model = NoisyOrModel([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], [[0.6, 0.4],</span>
<span class="sd">        ...                                                      [0.2, 0.4, 0.7],</span>
<span class="sd">        ...                                                      [0.1, 0. 4]])</span>
<span class="sd">        &gt;&gt;&gt; model.add_variables([&#39;x4&#39;], [3], [0.1, 0.4, 0.2])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">variables</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inhibitor_probability</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                <span class="n">inhibitor_probability</span> <span class="o">=</span> <span class="p">[</span><span class="n">inhibitor_probability</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">variables</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cardinality</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Size of variables and cardinality should be same&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">any</span><span class="p">(</span><span class="n">cardinal</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">prob_array</span><span class="p">)</span> <span class="k">for</span> <span class="n">prob_array</span><span class="p">,</span> <span class="n">cardinal</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">inhibitor_probability</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">))</span> <span class="ow">or</span> \
                <span class="nb">len</span><span class="p">(</span><span class="n">cardinality</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">inhibitor_probability</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Size of variables and inhibitor_probability should be same&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">item</span> <span class="o">&lt;=</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">inhibitor_probability</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Probability values should be between 0 and 1(both inclusive).&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="n">variables</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">inhibitor_probability</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">inhibitor_probability</span><span class="p">)</span></div>

<div class="viewcode-block" id="NoisyOrModel.del_variables"><a class="viewcode-back" href="../../../models.html#pgmpy.models.NoisyOrModel.NoisyOrModel.del_variables">[docs]</a>    <span class="k">def</span> <span class="nf">del_variables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variables</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deletes variables from the NoisyOrModel.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        variables: list, tuple, dict (array like)</span>
<span class="sd">            list of variables to be deleted.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import NoisyOrModel</span>
<span class="sd">        &gt;&gt;&gt; model = NoisyOrModel([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;], [2, 3, 2], [[0.6, 0.4],</span>
<span class="sd">        ...                                                      [0.2, 0.4, 0.7],</span>
<span class="sd">        ...                                                      [0.1, 0. 4]])</span>
<span class="sd">        &gt;&gt;&gt; model.del_variables([&#39;x1&#39;])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">variables</span><span class="p">]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="k">else</span> <span class="nb">set</span><span class="p">(</span><span class="n">variables</span><span class="p">)</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">index</span> <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">variable</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="k">if</span> <span class="n">variable</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">inhibitor_probability</span> <span class="o">=</span> <span class="p">[</span><span class="n">prob_array</span> <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">prob_array</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">inhibitor_probability</span><span class="p">)</span>
                                      <span class="k">if</span> <span class="n">index</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">]</span></div></div>

    <span class="c1">#</span>
    <span class="c1"># def out_prob(self, func):</span>
    <span class="c1">#     &quot;&quot;&quot;</span>
    <span class="c1">#     Compute the conditional probability of output variable</span>
    <span class="c1">#     given all other variables [P(X|U)] where X is the output</span>
    <span class="c1">#     variable and U is the set of input variables.</span>
    <span class="c1">#</span>
    <span class="c1">#     Parameters</span>
    <span class="c1">#     ----------</span>
    <span class="c1">#     func: function</span>
    <span class="c1">#         The deterministic function which maps input to the</span>
    <span class="c1">#         output.</span>
    <span class="c1">#</span>
    <span class="c1">#     Returns</span>
    <span class="c1">#     -------</span>
    <span class="c1">#     List of tuples. Each tuple is of the form (state, probability).</span>
    <span class="c1">#     &quot;&quot;&quot;</span>
    <span class="c1">#     states = []</span>
    <span class="c1">#     from itertools import product</span>
    <span class="c1">#     for u in product([(values(var)) for var in self.variables]):</span>
    <span class="c1">#         for state in product([(values(var) for var in self.variables)]):</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>